package deadletterexchange;

import com.rabbitmq.client.*;

import java.nio.charset.StandardCharsets;

// 死信队列学习 - 生产者
public class Producer {

    public static final String NORMAL_EXCHANGE_NAME = "normal_exchange";

    static Connection createConnection() throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.208.134");
        factory.setUsername("admin");
        factory.setPassword("123");
        return factory.newConnection();
    }

    public static void main(String[] args) throws Exception {

        try (Connection connection = createConnection();
             Channel channel = connection.createChannel()) {
            // 声明交换机
            channel.exchangeDeclare(NORMAL_EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
            // 设置消息的TTL(Time to Live)
            //AMQP.BasicProperties basicProperties = new AMQP.BasicProperties().builder().expiration("10000").build();
            // 发送消息
            for (int i = 1; i < 11; i++) {
                String message = "info" + i;
                channel.basicPublish(NORMAL_EXCHANGE_NAME, "zhangsan", null, message.getBytes(StandardCharsets.UTF_8));
                System.out.println("生产者发送消息: " + message);
            }
        }

    }
}
